<?php


/**
 * Description of AdminPresenter
 *
 * @author David Dusek
 */
use Nette\Application\UI\Form;

class ConfigPresenter extends AdminPresenter
{
    public function renderDefault() {
        $this->isAllowed("edit", TRUE);
        $this->template->title = $this->getTitle("title", "settings");
    }
    
    /**
     * Formular pro globalni nastaveni. 
     */
    protected function createComponentConfigForm() {
        $form = new Form();
        
        $layoutsOptions = $this->core->runHook("hookGetLayouts", null, 'ConfigModule');
        $stylesheetOptions = $this->core->runHook("hookGetStylesheets", array("/CMS/css"), 'ConfigModule');
        
        $form->addSelect("language", $this->translate('lang'), $this->dictionary->availableLangs);
        $form->addSelect("adminLayout", $this->translate('adminLayout'), $layoutsOptions);
        $form->addSelect("loginLayout", $this->translate('loginLayout'), $layoutsOptions);
        $form->addSelect("stylesheet", $this->translate('stylesheet'), $stylesheetOptions);
        
        $form->addSubmit('save', $this->translate("save"));
        $form->onSuccess[] = callback($this, 'configFormSubmitted');
        
        $form->setDefaults(array(
            "language" => $this->globalConfig["language"],
            "adminLayout" => $this->globalConfig["adminLayout"],
            "loginLayout" => $this->globalConfig["loginLayout"],
            "stylesheet" => $this->globalConfig["stylesheet"]
        ));
        
        return $form;
    }
    
    /**
     * Zpracovani formulare pro globalni zpracovani.
     * @param Form $form 
     */
    public function configFormSubmitted(Form $form) {
        $values = $form->getValues();
        
        $status = $this->core->runHook("hookWriteConfig", $values, 'ConfigModule');
        if ($status) {
            $this->flashMessage($this->translate("configEditSucc"));
        } else {
            $this->flashMessage($this->translate("configEditErr"));
        }
    }
}
